/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.cms.web.front;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.thinkgem.jeesite.common.constant.Constant;
import com.thinkgem.jeesite.common.utils.Collections3;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.gt.entity.GtInfoDeliver;
import com.thinkgem.jeesite.modules.gt.entity.GtWorkReport;
import com.thinkgem.jeesite.modules.gt.service.GtAnalysisService;
import com.thinkgem.jeesite.modules.gt.service.GtInfoDeliverService;
import com.thinkgem.jeesite.modules.gt.service.GtWorkReportService;
import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 测试Controller
 * @author ThinkGem
 * @version 2014-02-28
 */
@Controller
@RequestMapping(value = "${frontPath}/weixin")
public class WeixinController extends BaseController {
	
	@Autowired
	private GtAnalysisService gtAnalysisService;
	
	@Autowired
	private GtWorkReportService gtWorkReportService;
	
	@Autowired
	private GtInfoDeliverService gtInfoDeliverService;
	
	/**
	 * 登录成功，进入手机端管理首页
	 */
	 @RequiresPermissions("user")
	 
	@RequestMapping(value = "selfAndSub")
	public String selfAndSub(Area area,HttpServletRequest request, HttpServletResponse response,String menuId, Model model) {
		
		String returnUrl="";
			
			User user = UserUtils.getUser();
			model.addAttribute("user", user);
			model.addAttribute("menuId", menuId);
			
			Map<String,String> map = new HashMap<String, String>();
			String code = user.getArea().getCode();
			map.put("code", code);
			
			String type = user.getArea().getType();
			map.put("level", type);
			
			// add by zad 2018-04-08 经济扶贫林总体进度  面积和人数
			Map<String,String> forestTotalRate = this.gtAnalysisService.forestTotalRate(map);
			model.addAttribute("forestTotalRate", forestTotalRate);
			// add by zad 2018-04-08 经济扶贫林总体进度
			/*Map<String,String> forestPeopleRate = this.gtAnalysisService.forestPeopleRate();
			model.addAttribute("forestPeopleRate", forestPeopleRate);*/
			
			// add by zad 2018-04-08 护林员岗位总体扶贫进度
			Map<String,String> postTotalRate = this.gtAnalysisService.postTotalRate(map);
			model.addAttribute("postTotalRate", postTotalRate);
			
			// add by zad 2018-04-08 企社总体扶贫进度          2018-04-20 企社不再区分是企业还是合作社
			// map.put("type", Constant.Company.COMPANY_TYPE_COMPANY);
			Map<String,String> companyTotalRate = this.gtAnalysisService.companyTotalRate(map);
			model.addAttribute("companyTotalRate", companyTotalRate);
			
			/*各个地市、区县所有进度统计 add by zad 2018-05-02*/
			map.put("clearType", "4");
			map.put("clearTypeTown", "5");
			map.put("clearTypeCountry", "6");
			// 龙头企业
			map.put("companyType", "1");
			// 合作社
			map.put("coopType", "2");
			List<Map<String,String>> fpRegionAllRateList = this.gtAnalysisService.fpRegionAllRate(map);
			
			// 经济林面积排名
			Map<String,String> rankForest = Collections3.sortList(fpRegionAllRateList, "forestRate", "forestArea");
			// 经济林人数排名
			Map<String,String> rankFp = Collections3.sortList(fpRegionAllRateList, "forestPeopleRate", "forestPeople");
			// 护林员岗位人数排名
			Map<String,String> rankPost = Collections3.sortList(fpRegionAllRateList, "postRate", "rangerPost");
			// 企业（合作社）排名
			Map<String,String> rankCompany = Collections3.sortList(fpRegionAllRateList, "companyRate", "leadCompany");
			// 企业排名
			Map<String,String> rankCompanyOnly = Collections3.sortList(fpRegionAllRateList, "companyRateOnly", "leadCompanyOnly");
			// 合作社排名
			Map<String,String> rankCoopOnly = Collections3.sortList(fpRegionAllRateList, "coopRateOnly", "leadCoopOnly");
			
			// 科技下乡排名
			Map<String,String> rankCountry = Collections3.sortList(fpRegionAllRateList, "countryRate", "total");
			
			// 县技术专家排名
			Map<String,String> rankClear = Collections3.sortList(fpRegionAllRateList, "clearRate", "countyTotal");
			// 乡镇技术员排名
			Map<String,String> rankTownClear = Collections3.sortList(fpRegionAllRateList, "clearTownRate", "townTotal");
			// 村技术明白人排名
			Map<String,String> rankCountryClear = Collections3.sortList(fpRegionAllRateList, "clearCountryRate", "countryTotal");
														
			for(Map<String,String> item : fpRegionAllRateList)
			{
				item.put("forestRate" + "Rank", rankForest.get(item.get("code")));
				item.put("forestPeopleRate" + "Rank", rankFp.get(item.get("code")));
				item.put("postRate" + "Rank", rankPost.get(item.get("code")));
				item.put("companyRate" + "Rank", rankCompany.get(item.get("code")));
				item.put("companyRateOnly" + "Rank", rankCompanyOnly.get(item.get("code")));
				item.put("coopRateOnly" + "Rank", rankCoopOnly.get(item.get("code")));
				
				
				item.put("countryRate" + "Rank", rankCountry.get(item.get("code")));
				
				item.put("clearRate" + "Rank", rankClear.get(item.get("code")));
				item.put("clearTownRate" + "Rank", rankTownClear.get(item.get("code")));
				item.put("clearCountryRate" + "Rank", rankCountryClear.get(item.get("code")));
			}
			
			
			model.addAttribute("fpRegionAllRateList", fpRegionAllRateList);
			
			
			GtWorkReport gtWorkReport = new GtWorkReport();
			
			if(null == gtWorkReport.getArea() || StringUtils.isBlank(gtWorkReport.getArea().getCode()))
			{
				gtWorkReport.setArea(UserUtils.getUser().getArea());
			}
			
			// 各地市工作月报
			List<GtWorkReport> workReportList = this.gtWorkReportService.findList(gtWorkReport);
			model.addAttribute("workReportList", workReportList);
			
			
			// 信息宣传不做限制，默认只展示当月提交的信息宣传
			GtInfoDeliver info = new GtInfoDeliver();
			info.setCreateDate(new Date());
			/*Area areaP = new Area();
			areaP.setCode(code);
			info.setArea(areaP);*/
			// 各地市信息宣传
			List<GtInfoDeliver> infoList = this.gtInfoDeliverService.findList(info);
			model.addAttribute("infoList", infoList);
			
			returnUrl="modules/gt/weixin/selfAndSub";
			
		return returnUrl;
	}
	
	 /**
	 * 登录成功，进入手机端管理首页
	 */
	 @RequestMapping(value = "self")
	public String self(Area area,HttpServletRequest request, HttpServletResponse response,String menuId, Model model) {
		
		 model.addAttribute("menuId", menuId);
			
		 Map<String,String> map = new HashMap<String, String>();
			
			User user = UserUtils.getUser();
			String code = user.getArea().getCode();
			map.put("code", code);
			String type = user.getArea().getType();
			map.put("level", type);
			
			
			model.addAttribute("user", user);
			model.addAttribute("menuId", menuId);
			
			
			// add by zad 2018-04-08 经济扶贫林总体进度  面积和人数
			/*Map<String,String> forestTotalRate = this.gtAnalysisService.forestTotalRate(map);
			model.addAttribute("forestTotalRate", forestTotalRate);*/
			// add by zad 2018-04-08 经济扶贫林总体进度
			/*Map<String,String> forestPeopleRate = this.gtAnalysisService.forestPeopleRate();
			model.addAttribute("forestPeopleRate", forestPeopleRate);*/
			
			// add by zad 2018-04-08 护林员岗位总体扶贫进度
			/*Map<String,String> postTotalRate = this.gtAnalysisService.postTotalRate(map);
			model.addAttribute("postTotalRate", postTotalRate);*/
			
			// add by zad 2018-04-08 企社总体扶贫进度          2018-04-20 企社不再区分是企业还是合作社
			// map.put("type", Constant.Company.COMPANY_TYPE_COMPANY);
			Map<String,String> companyTotalRate = this.gtAnalysisService.companyTotalRate(map);
			model.addAttribute("companyTotalRate", companyTotalRate);
			
			
			// 不再查询全省，根据用户的实际单位来查询
			// map.put("code", "sd");
			// map.put("level", "2");
			
			// add by zad 2018-04-08 经济扶贫林总体进度
			Map<String,String> forestTotalRate = this.gtAnalysisService.forestTotalRate(map);
			forestTotalRate.put("forestAreaName", "经济林扶贫（面积：亩）");
			forestTotalRate.put("forestPeopleName", "经济林扶贫（人数）");
			model.addAttribute("forestTotalRate", forestTotalRate);
			
			
			// add by zad 2018-04-08 护林员岗位总体扶贫进度
			Map<String,String> postTotalRate = this.gtAnalysisService.postTotalRate(map);
			postTotalRate.put("postName", "护林员岗位扶贫");
			model.addAttribute("postTotalRate", postTotalRate);
			
			// add by zad 2018-04-08 林业政策总体扶贫进度
			Map<String,String> policyTotal = this.gtAnalysisService.policyTotal(map);
			policyTotal.put("policyName", "林业政策扶贫");
			model.addAttribute("policyTotal", policyTotal);
			
			// add by zad 2018-04-08 龙头企业（合作社）总体扶贫进度
			/*Map<String,String> companyTotal = this.gtAnalysisService.companyTotal(map);
			companyTotal.put("companyName", "富民示范龙头企业（合作社）扶贫");
			model.addAttribute("companyTotal", companyTotal);*/
			
			// add by zad 2018-05-13 龙头企业总体扶贫进度单独统计
			map.put("type", Constant.Company.COMPANY_TYPE_COMPANY);
			Map<String,String> companyTotalOnly = this.gtAnalysisService.companyTotal(map);
			companyTotalOnly.put("companyName", "林业龙头企业扶贫人数");
			model.addAttribute("companyTotalOnly", companyTotalOnly);
			
			// add by zad 2018-04-08 合作社总体扶贫进度单独统计
			map.put("type", Constant.Company.COMPANY_TYPE_COOP);
			Map<String,String> coopTotalOnly = this.gtAnalysisService.companyTotal(map);
			coopTotalOnly.put("coopName", "林业合作社扶贫人数");
			model.addAttribute("coopTotalOnly", coopTotalOnly);
			
			
			// add by zad 2018-04-26 科技下乡地区总体扶贫进度，省2次，地市4次、区县常态化
			/*Map<String,String> regionCompanyTotal = this.gtAnalysisService.regionCountryTotal(map);
			regionCompanyTotal.put("regionCountryName", "科技下乡扶贫（地市统计）");
			model.addAttribute("regionCompanyTotal", regionCompanyTotal);*/
			
			// add by zad 2018-04-26 技术培训总体扶贫进度，每个地市要求4次科技下乡活动
			Map<String,String> trainTotal = this.gtAnalysisService.trainTotal(map);
			trainTotal.put("trainName", "技术培训");
			model.addAttribute("trainTotal", trainTotal);
			
			// add by zad 2018-04-26 技术明白人总体扶贫进度，每个地市要求4次科技下乡活动
			/*Map<String,String> clearTotal = this.gtAnalysisService.clearTotal(map);
			clearTotal.put("clearName", "技术明白人");
			model.addAttribute("clearTotal", clearTotal);*/
			
			map.put("clearType", "4");
			map.put("clearTypeTown", "5");
			map.put("clearTypeCountry", "6");
			Map<String,String> clearTotal = this.gtAnalysisService.clearRegionTotal(map);
			clearTotal.put("clearName", "县技术明白人");
			clearTotal.put("clearTownName", "乡镇技术员");
			clearTotal.put("clearCountryName", "村技术明白人");
			model.addAttribute("clearTotal", clearTotal);
			
			
			// add by zad 2018-04-26 当月工作月报进度
			Map<String,String> reportTotal = this.gtAnalysisService.reportTotal(map);
			reportTotal.put("reportName", "工作月报（当月）");
			model.addAttribute("reportTotal", reportTotal);
			
			// add by zad 2018-04-26 当月工作月报进度
			Map<String,String> deliverTotal = this.gtAnalysisService.deliverTotal(map);
			deliverTotal.put("deliverName", "信息宣传");
			model.addAttribute("deliverTotal", deliverTotal);
			
			// 科技下乡查询，与下级查询调用同一个sql
			// map.put("level", String.valueOf(Integer.parseInt(type) - 1));
			map.put("level", String.valueOf(type));
			map.put("queryType", "self");
			Map<String,String> regionCompanyTotal = this.gtAnalysisService.countryRegionRate(map).get(0);
			regionCompanyTotal.put("regionCountryName", "科技下乡");
			model.addAttribute("regionCompanyTotal", regionCompanyTotal);
			
			return "modules/gt/weixin/gtOverAll";
	}
	 
	 
	 
	 @RequestMapping(value = "dateInput")
	 public String dateInput(HttpServletRequest request,String menuId, Model model) {
		 
		 User user = UserUtils.getUser();
			model.addAttribute("user", user);
			model.addAttribute("menuId", menuId);
			
			Map<String,String> map = new HashMap<String, String>();
			String code = user.getArea().getCode();
			map.put("code", code);
			
			String type = user.getArea().getType();
			map.put("level", type);
			
			// add by zad 2018-04-08 经济扶贫林总体进度  面积和人数
			Map<String,String> forestTotalRate = this.gtAnalysisService.forestTotalRate(map);
			model.addAttribute("forestTotalRate", forestTotalRate);
			// add by zad 2018-04-08 经济扶贫林总体进度
			/*Map<String,String> forestPeopleRate = this.gtAnalysisService.forestPeopleRate();
			model.addAttribute("forestPeopleRate", forestPeopleRate);*/
			
			// add by zad 2018-04-08 护林员岗位总体扶贫进度
			Map<String,String> postTotalRate = this.gtAnalysisService.postTotalRate(map);
			model.addAttribute("postTotalRate", postTotalRate);
			
			// add by zad 2018-04-08 企社总体扶贫进度          2018-04-20 企社不再区分是企业还是合作社
			// map.put("type", Constant.Company.COMPANY_TYPE_COMPANY);
			Map<String,String> companyTotalRate = this.gtAnalysisService.companyTotalRate(map);
			model.addAttribute("companyTotalRate", companyTotalRate);
		 
		 return "modules/gt/weixin/dateInput";
	 }
			
	 
	/**
	 * 
	 * @param signature 微信加密签名，signature结合了开发者填写的token参数和请求中的timestamp参数、nonce参数。 
	 * @param timestamp 时间戳
	 * @param nonce 随机数 
	 * @param echostr 随机数 
	 * @return
	 */
	/*@RequestMapping(value = "", method = RequestMethod.GET)
	@ResponseBody
	public String get(String signature, String timestamp, String nonce, String echostr, HttpServletRequest request) {
		
		System.out.println("=============================================== get start");
		for (Object o : request.getParameterMap().keySet()){
			System.out.println(o + " = " + request.getParameter((String)o));
		}
		System.out.println("=============================================== get end");
		
        // 通过检验signature对请求进行校验，若校验成功则原样返回echostr，表示接入成功，否则接入失败  
        if (WiexinSignUtil.checkSignature(signature, timestamp, nonce)) {  
        	return echostr;
        }

		return "";
	}

	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	public String post(String signature, String timestamp, String nonce, String echostr, HttpServletRequest request) {
		System.out.println("=============================================== post start");
		for (Object o : request.getParameterMap().keySet()){
			System.out.println(o + " = " + request.getParameter((String)o));
		}
		System.out.println("=============================================== post end");
		StringBuilder result = new StringBuilder();
		result.append("<xml>" +
				"<ToUserName><![CDATA[toUser]]></ToUserName>" +
				"<FromUserName><![CDATA[fromUser]]></FromUserName>" +
				"<CreateTime>12345678</CreateTime>" +
				"<MsgType><![CDATA[text]]></MsgType>" +
				"<Content><![CDATA[你好]]></Content>" +
				"</xml>");
		return result.toString();
	}*/
	
	
}
